using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Security;

public class InappController : MonoBehaviour, IStoreListener
{
	public delegate void BuyIAPSuccess(int type);

	private InappController singleton;

	private static bool created;

	public static IStoreController m_StoreController;

	public static IExtensionProvider m_StoreExtensionProvider;

	public const string _7000gold = "com.unimob.stickman.ghost.ninja.warrior.7000golds";

	public const string _40000gold = "com.unimob.stickman.ghost.ninja.warrior.40000golds";

	public const string _90000gold = "com.unimob.stickman.ghost.ninja.warrior.90000golds";

	public const string _200000gold = "com.unimob.stickman.ghost.ninja.warrior.200000golds";

	public const string _450000gold = "com.unimob.stickman.ghost.ninja.warrior.450000golds";

	public const string _fairy = "com.unimob.stickman.ghost.ninja.warrior.fairy";

	public const string leaderboardID = "CgkI-erF35AMEAIQAQ";

	public static bool _isInit;

	public static event BuyIAPSuccess BuySuccessfully;

	private void Awake()
	{
		if (singleton != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		singleton = this;
		Object.DontDestroyOnLoad(base.gameObject);
	}

	private void Start()
	{
		if (m_StoreController == null)
		{
			Init();
		}
	}

	public void Init()
	{
		if (!IsInitialized())
		{
			ConfigurationBuilder configurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.7000golds", ProductType.Consumable);
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.40000golds", ProductType.Consumable);
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.90000golds", ProductType.Consumable);
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.200000golds", ProductType.Consumable);
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.450000golds", ProductType.Consumable);
			configurationBuilder.AddProduct("com.unimob.stickman.ghost.ninja.warrior.fairy", ProductType.NonConsumable);
			UnityPurchasing.Initialize(this, configurationBuilder);
		}
	}

	public static bool IsInitialized()
	{
		return m_StoreController != null && m_StoreExtensionProvider != null;
	}

	public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
	{
		m_StoreController = controller;
		m_StoreExtensionProvider = extensions;
	}

	public void OnInitializeFailed(InitializationFailureReason error)
	{
		UnityEngine.Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
	}

	public static void RestorePurchases()
	{
		if (!IsInitialized())
		{
			UnityEngine.Debug.Log("RestorePurchases FAIL. Not initialized.");
		}
		else if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer)
		{
			UnityEngine.Debug.Log("RestorePurchases started ...");
			IAppleExtensions extension = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
			extension.RestoreTransactions(delegate(bool result)
			{
				MNPopup mNPopup = new MNPopup("Successful", "Your in-app purchases restore completed!");
				mNPopup.AddAction("Ok", delegate
				{
				});
				mNPopup.AddDismissListener(delegate
				{
				});
				mNPopup.Show();
				UnityEngine.Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
			});
		}
		else
		{
			UnityEngine.Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
		}
	}

	public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
	{
		bool flag = true;
		CrossPlatformValidator crossPlatformValidator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.identifier);
		try
		{
			IPurchaseReceipt[] array = crossPlatformValidator.Validate(e.purchasedProduct.receipt);
			UnityEngine.Debug.Log("Receipt is valid. Contents:");
			IPurchaseReceipt[] array2 = array;
			foreach (IPurchaseReceipt purchaseReceipt in array2)
			{
				UnityEngine.Debug.Log(purchaseReceipt.productID);
				UnityEngine.Debug.Log(purchaseReceipt.purchaseDate);
				UnityEngine.Debug.Log(purchaseReceipt.transactionID);
			}
		}
		catch (IAPSecurityException)
		{
			UnityEngine.Debug.Log("Invalid receipt, not unlocking content");
			flag = false;
		}
		if (flag)
		{
			BuySuccess(e.purchasedProduct.definition.id);
		}
		else if (Application.systemLanguage == SystemLanguage.Vietnamese)
		{
			MNPopup mNPopup = new MNPopup("Không thành công!", "Có vẻ bạn đang cố gắng thanh toán bằng một phương thức không hợp lệ!!!");
			mNPopup.AddAction("Ok", delegate
			{
			});
			mNPopup.AddDismissListener(delegate
			{
			});
			mNPopup.Show();
		}
		else
		{
			MNPopup mNPopup2 = new MNPopup("Error!", "Payment failed!!!");
			mNPopup2.AddAction("Ok", delegate
			{
			});
			mNPopup2.AddDismissListener(delegate
			{
			});
			mNPopup2.Show();
		}
		return PurchaseProcessingResult.Complete;
	}

	public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
	{
		UnityEngine.Debug.Log($"OnPurchaseFailed: FAIL. Product: '{product.definition.storeSpecificId}', PurchaseFailureReason: {failureReason}");
	}

	public static void Buy(string productId)
	{
		if (IsInitialized())
		{
			Product product = m_StoreController.products.WithID(productId);
			if (product != null && product.availableToPurchase)
			{
				UnityEngine.Debug.Log($"Purchasing product asychronously: '{product.definition.id}'");
				m_StoreController.InitiatePurchase(product);
			}
			else
			{
				UnityEngine.Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
			}
		}
		else
		{
			UnityEngine.Debug.Log("BuyProductID FAIL. Not initialized.");
		}
	}

	public static void ConsumedProduct(string productId)
	{
		if (IsInitialized())
		{
			Product product = m_StoreController.products.WithID(productId);
			if (product != null && product.availableToPurchase)
			{
				UnityEngine.Debug.Log($"Purchasing product asychronously: '{product.definition.id}'");
				m_StoreController.InitiatePurchase(product);
			}
			else
			{
				UnityEngine.Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
			}
		}
		else
		{
			UnityEngine.Debug.Log("BuyProductID FAIL. Not initialized.");
		}
	}

	public static void BuySuccess(string inAppID)
	{
		if (inAppID != null)
		{
			if (!(inAppID == "com.unimob.stickman.ghost.ninja.warrior.7000golds"))
			{
				if (!(inAppID == "com.unimob.stickman.ghost.ninja.warrior.40000golds"))
				{
					if (!(inAppID == "com.unimob.stickman.ghost.ninja.warrior.90000golds"))
					{
						if (!(inAppID == "com.unimob.stickman.ghost.ninja.warrior.200000golds"))
						{
							if (!(inAppID == "com.unimob.stickman.ghost.ninja.warrior.450000golds"))
							{
								if (inAppID == "com.unimob.stickman.ghost.ninja.warrior.fairy")
								{
									GameManager.Tinker = 1;
								}
							}
							else
							{
								GameManager.TotalCoin += 450000;
							}
						}
						else
						{
							GameManager.TotalCoin += 200000;
						}
					}
					else
					{
						GameManager.TotalCoin += 90000;
					}
				}
				else
				{
					GameManager.TotalCoin += 40000;
				}
			}
			else
			{
				GameManager.TotalCoin += 7000;
			}
		}
		SaveGameController.SaveGame();
	}
}
